/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.units.GoogleCommon;

import java.io.Serializable;

/**
 *
 * @author giorgio
 */
public class Location implements Serializable {

  private Double lat = 0.0;
  private Double lng = 0.0;

  public float getLat() {
    return lat.floatValue();
  }

  public double getLatDouble() {
    return lat.doubleValue();
  }

  public void setLng(double lng) {
    lng = lng % 360;

    this.lng = backUpExcedence(lng, 180, true);
  }

  public void setLat(double lat) {
    lat = lat % 360;

    if (lat > 180) {
      lat = 180 - lat;
    }
    this.lat = backUpExcedence(lat, 90, false);
  }

  private double backUpExcedence(double value, int maxValue, boolean negate) {
    boolean exceedsMax = Math.abs(value) > maxValue;
    if (exceedsMax) {
      double signedMax = Math.signum(value) * maxValue;
      double excess =  value - signedMax;
      
      value = signedMax - excess;
      if (negate) {
        value = -value;
      }
    }
    return value;
  }

  public float getLng() {
    return lng.floatValue();
  }

  public double getLngDouble() {
    return lng.doubleValue();
  }

  @Override
  public String toString() {
    return lat.toString() + "," + lng.toString();
  }

}
